//
// RS Game Framework
// Copyright © 2009 Jedd Haberstro
// jhaberstro@gmail.com
// 
// $Id:
//

#include <rs/Thread.hpp>
#include <rs/unittest/UnitTest.hpp>
#include <vector>

namespace
{
    using namespace rs;
    using namespace rs::unittest;
    
    enum { kExpected = 10, kInitial = 0 };
    
    void ThreadUsageFunc(void* context) {
        int* arg = static_cast< int* >(context);
        *arg = kExpected;
    }
    
    RS_TEST_CASE(rs::Thread, Usage)
    {
        enum { kThreadCount = 4 };
        std::vector< Thread > threads;
        int args[kThreadCount] = { kInitial };
        
        for (int i = 0; i < kThreadCount; ++i) {
            threads.push_back(Thread(ThreadUsageFunc));
        }
        
        for (int i = 0; i < kThreadCount; ++i) {
            threads[i].Start(static_cast< void* >(&args[i]));
        }
        
        for (int i = 0; i < kThreadCount; ++i) {
            threads[i].Join();
        }
        
        for (int i = 0; i < kThreadCount; ++i) {
            RS_ASSERT(args[i] == kExpected, "arg does not equal expected value");
        }
    }
    
    
    void ThreadIDFunc(void* context) {
        ThreadID* id = static_cast< ThreadID* >(context);
        *id = Thread::GetCurrentID();
    }
    
    RS_TEST_CASE(rs::Thread, ThreadID)
    {
        Thread thread(ThreadIDFunc);
        ThreadID result;
        thread.Start(static_cast< void* >(&result));
        ThreadID id = thread.GetID();
        thread.Join();
        
        RS_ASSERT(id == result, "Thread::GetCurrentID failed");
        RS_ASSERT(id != Thread::GetCurrentID(), "Thread::GetCurrentID failed");
    }
}